package org.example.novel.asp;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.example.novel.annotation.Log;
import org.example.novel.enums.TypeEnum;
import org.example.novel.exception.AuthException;
import org.example.novel.utlis.JwtUtils;
import org.example.novel.utlis.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

@Aspect
public class LogAsp {

    @Autowired
    HttpServletRequest request;

    @Pointcut("@annotation(org.example.novel.annotation.Log)")
    public void pointcut(){}

    @Before("pointcut()")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();

        Log annotation = signature.getMethod().getAnnotation(Log.class);

        if (annotation!=null){

            String name = annotation.name();
            TypeEnum type = annotation.type();
            String token = RequestUtils.getValueByRequestHandersAndCookies("token");
            if (token!=null){
                Object quanxian = JwtUtils.parseToken(token).get("权限");
                if (!name.equals(quanxian)){
                    throw new AuthException("权限不足");
                }
            }

        }

    }

    @After("pointcut()")
    public void after(){

    }

}
